package cn.stronglink.asset.module.basicdata.tRoom.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.plugins.Page;
import cn.stronglink.asset.model.TRoom;
import cn.stronglink.asset.module.basicdata.storeRoom.service.StoreRoomService;
import cn.stronglink.asset.module.basicdata.tRoom.service.TRoomService;
import cn.stronglink.asset.module.basicdata.tRoom.vo.RoomTreeNodeVo;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.config.Resources;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.support.HttpCode;

@Controller
@RequestMapping(value = "basic/room")
public class TRoomController extends AbstractController {

	@Autowired
	private TRoomService tRoomService;
	@Autowired
	private StoreRoomService storeRoomService;

	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		@SuppressWarnings("unchecked")
		Page<TRoom> page = (Page<TRoom>) super.getPage(params);
		Page<TRoom> data = tRoomService.queryTRoom(page, params);
		return setSuccessModelMap(modelMap, data);
	}
	
	@RequestMapping(value = "queryRoomList")
	public ResponseEntity<ModelMap> queryRoomList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {		
		List<TRoom> data = tRoomService.queryRoomList();
		return setSuccessModelMap(modelMap, data);
	}
	/**
	 * 通过参数查询机房列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "queryRoomListByParams")
	public ResponseEntity<ModelMap> queryRoomListByParams(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String,Object> params) {		
		List<TRoom> data = tRoomService.queryRoomListByParams(params);
		return setSuccessModelMap(modelMap, data);
	}
	
	@RequestMapping(value = "queryRoomU")
	public ResponseEntity<ModelMap> queryRoomU(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		List<HashMap<String,Object>> data = tRoomService.queryRoomU(params);
		return setSuccessModelMap(modelMap, data);
	}
	
	@OperateLog(module = "机房管理",desc="添加机房信息", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody TRoom tRoom) {
		tRoomService.insertTRoom(tRoom);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "机房管理",desc="修改机房信息", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody TRoom tRoom) {
		tRoomService.insertTRoom(tRoom);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "机房管理",desc="删除机房信息", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		int isUsed =storeRoomService.getIsUsedRoom(id);
		if(isUsed > 0){		
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("IsUsedRoom"));
		}
		tRoomService.removeTRoom(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 查询树形机房和机柜
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "queryRoomListTree")
	public ResponseEntity<ModelMap> queryRoomListTree(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {		
		List<RoomTreeNodeVo> data = tRoomService.queryRoomListTree();
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 查询所有库房和机房
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "queryAllRoomList")
	public ResponseEntity<ModelMap> queryAllRoomList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {		
		List<TRoom> data = tRoomService.queryAllRoomList();
		return setSuccessModelMap(modelMap, data);
	}

}
